"""
Copyright 2009, Thomas Dejanovic, Jay Shurtz.
 
This is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this software; if not, write to the Free
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA, or see the FSF site: http://www.fsf.org.

Base module for all physical HatchNode modules.
"""

id = "$Id: hatch_node_physical.py 667 2010-07-01 00:12:17Z jayshurtz $"
# $URL: http://hatch.googlecode.com/svn/tags/taggle_release_3.0/hatch/hatch_nodes/hatch_node_physical.py $
# $Author: jayshurtz $
version = " version ".join(id.split()[1:3])


import to_hatch_structure
from hatch_node import *
from hatch_constants import HATCH_LEAF, PACKED


class HatchNodePhysical(HatchNode):
    """ Base class for physical instances of a HatchNode, such as registers, structs, and packs.
        This class is used to enforce the existence of physical properties (such as 'hatch_leaf') in every physical class.
        While this may seem like an extra class at first, it is a useful place to keep any constants, properties, or methods
        that are common to all the physical classes but do not belong in the (already crowded) 'hatch node' class itself.
    """
    def __init__(self, name, *childNodes, **properties):
        """ Create a physical hatch node instance.
            Note: To have machine-generated hatch code, we want to be able to 
            instanciate any class generically and without properties, so any *required* properties 
            should ALWAYS have a default value in __init__() ('name' is a class attribute, not a property.)
        """
        HatchNode.__init__(self, name, *childNodes, **properties)
        self.properties[HATCH_LEAF] = properties.get(HATCH_LEAF, len(childNodes) == 0)  # Default to leaf node if no child nodes, non-leaf if child nodes.


if __name__ == "__main__":

    h0 = HatchNodePhysical('h0', bit_width=8)
    h1 = HatchNodePhysical('h1', bit_width=10)
    h2 = HatchNodePhysical('h2', bit_width=24)
    h3 = HatchNodePhysical('h3', bit_width=32)

    h0h1 = HatchNodePhysical('h0h1', h0, h1)
    h2h0h1 = HatchNodePhysical('h2h0h1', h2, h0h1)
    h3h2h0h1 = HatchNodePhysical('h3h2h0h1', h3, h2h0h1, prop='prop')
    #h0h1h2h3 = HatchNodePhysical('h0h1h2h3', h0, h1, h2, h3)

    hatchNodes = [h0, h1, h2, h3, h0h1, h2h0h1, h3h2h0h1]

    for hatchNode in hatchNodes:
        print hatchNode.info()
        raw_input()

    s = h3h2h0h1.copy('s', h3h2h0h1)    # Test .copy()
    print s.info()
    print h3h2h0h1.info()


